#include "mainpagewindow.h"
#include "ui_mainpagewindow.h"

MainPageWindow::MainPageWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainPageWindow),
    currentActiveForm(nullptr)
{
    mainWindow = parent;
    ui->setupUi(this);
    ui->widget_EmployeeManage->hide();
    ui->widget_punchCardManagement->hide();
    QRect r = ui->widget_6->geometry();
    ui->widget_6->setGeometry(r.x(), 80, r.width(), r.height());
    this->setWindowTitle("桥洞科技考勤后台管理");
    this->setWindowIcon(QIcon(":/img/aaa.png"));
}

MainPageWindow::~MainPageWindow()
{
    delete ui;
}

void MainPageWindow::on_btnEmployeeManage_clicked(bool checked)
{
    QRect r = ui->widget_6->geometry();
    if(checked){
        ui->widget_EmployeeManage->show();
        ui->widget_6->setGeometry(r.x(), 160, r.width(), r.height());
    }else{
        ui->widget_EmployeeManage->hide();
        ui->widget_6->setGeometry(r.x(), 80, r.width(), r.height());
    }
}


void MainPageWindow::setBtnCheacked(bool flag){
    ui->btnEmployeeAdd->setChecked(flag);
    ui->btnEmployeeQuery->setChecked(flag);
    ui->btnAttendanceSetting->setChecked(flag);
    ui->btnCheckInRecordQuery->setChecked(flag);
    ui->btnEmployeeLeave->setChecked(flag);
    ui->btnPunchCardManagement->setChecked(flag);
}


void MainPageWindow::on_btnPunchCardManagement_clicked(bool checked)
{
    if(checked){
        ui->widget_punchCardManagement->show();
    }else{
        ui->widget_punchCardManagement->hide();
    }
}

void MainPageWindow::on_commandLinkButton_clicked()
{
    this->close();
    mainWindow->show();
}

void MainPageWindow::switchForm(QWidget* newForm)
{
    if (!newForm) {
        return;
    }

    // 1. 删除旧的活动表单
    if (currentActiveForm) {
        delete currentActiveForm;
        currentActiveForm = nullptr; // 关键：删除后立即置空
    }

    // 2. 确保 widget_3 使用栅格布局 (这部分保持不变)
    QGridLayout* gridLayout = dynamic_cast<QGridLayout*>(ui->widget_3->layout());
    if (!gridLayout) {
        gridLayout = new QGridLayout(ui->widget_3);
        gridLayout->setContentsMargins(0, 0, 0, 0);
        gridLayout->setSpacing(0);
    }

    // 3. 配置并添加新表单
    newForm->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    gridLayout->addWidget(newForm, 0, 0);

    // 4. 更新当前活动表单指针
    currentActiveForm = newForm;
}

void MainPageWindow::on_btnAttendanceSetting_clicked(bool checked)
{
    if(checked){
        setBtnCheacked(false); // 取消其他按钮的选中状态
        ui->btnAttendanceSetting->setChecked(true); // 确保当前按钮被选中

        // --- 在这里添加切换表单的逻辑 ---
        attendanceruleform = new attendanceruleForm(ui->widget_3);
        if (attendanceruleform != nullptr) {
            switchForm(attendanceruleform);
        }
    }
}

// 员工添加按钮
void MainPageWindow::on_btnEmployeeAdd_clicked(bool checked)
{
    if(checked){
        setBtnCheacked(false);
        ui->btnEmployeeAdd->setChecked(true);

        // --- 在这里添加切换表单的逻辑 ---
        regform = new regForm(ui->widget_3);
        if (regform != nullptr) {
            switchForm(regform);
        }
    }
}

// 员工查询按钮
void MainPageWindow::on_btnEmployeeQuery_clicked(bool checked)
{
    if(checked){
        setBtnCheacked(false);
        ui->btnEmployeeQuery->setChecked(true);

        // --- 在这里添加切换表单的逻辑 ---
        employeeQueryForm = new EmployeeQueryForm(ui->widget_3);
        if (employeeQueryForm != nullptr) {
            switchForm(employeeQueryForm);
        }
    }
}

// 签到记录查询按钮
void MainPageWindow::on_btnCheckInRecordQuery_clicked(bool checked)
{
    if(checked){
        setBtnCheacked(false);
        ui->btnCheckInRecordQuery->setChecked(true);

        // --- 在这里添加切换表单的逻辑 ---
        checkInRecordQueryForm = new CheckInRecordQueryForm(ui->widget_3);
        if (checkInRecordQueryForm != nullptr) {
            switchForm(checkInRecordQueryForm);
        }
    }
}

// 员工请假按钮
void MainPageWindow::on_btnEmployeeLeave_clicked(bool checked)
{
    if(checked){
        setBtnCheacked(false);
        ui->btnEmployeeLeave->setChecked(true);

        // --- 在这里添加切换表单的逻辑 ---
        employeeLeaveForm = new EmployeeLeaveForm(ui->widget_3);
        if (employeeLeaveForm != nullptr) {
            switchForm(employeeLeaveForm);
        }
    }
}
